﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_NewsTypeList : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    NewsDao nd = new NewsDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ViewState["pageindex"] = "0";
            lbpre.Visible = false;
            lbnext.Visible = false;
            ddltype.Items.Clear();
            ddltype.Items.Add("--请选择--");
            BindData();
        }
    }

    protected void IndexChanging(object sender, EventArgs e)
    {
        string strCommand = ((LinkButton)sender).CommandArgument.ToString();
        int pageindex = int.Parse(ViewState["pageindex"].ToString());
        if (strCommand == "pre")
        {
            pageindex = pageindex - 1;
        }
        else
        {
            pageindex = pageindex + 1;
        }
        ViewState["pageindex"] = pageindex;
        BindData();
    }

    private void BindData()
    {
        try
        {
            if (em.Decript(Request.QueryString["tid"]) == null)
            {
                Response.Redirect("../index.aspx");
            }
        }
        catch
        {
            Response.Redirect("../index.aspx");
        }
        string tid = em.Decript(Request.QueryString["tid"]);
        try
        {
            int id = Convert.ToInt32(tid);
        }
        catch
        {
            Response.Redirect("../index.aspx");
        }
        ddltype.Items.Clear();
        ddltype.Items.Add("--请选择--");
        switch (tid)
        {
            case "1": labtitle.Text = "热闹集市"; break;
            case "2": labtitle.Text = "本地信息"; break;
            case "3": labtitle.Text = "原创心情"; break;
            case "4": labtitle.Text = "艺术地带"; break;
            case "5": labtitle.Text = "教育天地"; break;
            case "6": labtitle.Text = "说三道四"; break;
        }
        if (tid == "1")
        {
            dt = nd.getModule(tid);
            ddltype.DataSource = dt;
            ddltype.DataTextField = "Mname";
            ddltype.DataValueField = "Mid";
            ddltype.DataBind();
            ddltype.Enabled = true;
            dt = nd.getBusNews();
        }
        else if (tid == "6")
        {
            dt = nd.getModule(tid);
            ddltype.DataSource = dt;
            ddltype.DataTextField = "Mname";
            ddltype.DataValueField = "Mid";
            ddltype.DataBind();
            ddltype.Enabled = true;
            dt = nd.getNews("6","5000");
        }
        else
        {
            dt = nd.getNews(tid.ToString(), "0", "");
        }
 	    //设置Title和搜索关键词
        Page.Title = ConfigurationSettings.AppSettings["title"] + "-"+ labtitle.Text;
        HtmlMeta metaKeyWords = new HtmlMeta();
        HtmlMeta metaDescription = new HtmlMeta();
        metaKeyWords.Name = "Keywords";
        metaKeyWords.Content = ConfigurationSettings.AppSettings["Keywords"];
        metaDescription.Name = "description";
        metaDescription.Content = ConfigurationSettings.AppSettings["description"];
        Header.Controls.Add(metaKeyWords);
        Header.Controls.Add(metaDescription);
        if (dt != null && dt.Rows.Count > 0)
        {
            DataView objView = dt.DefaultView;
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = objView;
            objPds.AllowPaging = true;
            objPds.PageSize = 19;
            objPds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());
            if (!objPds.IsFirstPage)
            {
                lbpre.Visible = true;
            }
            else
            {
                lbpre.Visible = false;
            }
            if (!objPds.IsLastPage)
            {
                lbnext.Visible = true;
            }
            else
            {
                lbnext.Visible = false;
            }
            dlnewslist.DataSource = objPds;
            dlnewslist.DataBind();
            for (int i = 0; i < objPds.Count; i++)
            {
                int size = Convert.ToInt32(ViewState["pageindex"]) * 19 + i;
                string id = dt.Rows[size][0].ToString();
                id = em.Encript(id);
                HyperLink hl = (HyperLink)dlnewslist.Items[i].FindControl("hlnews") as HyperLink;
                while (hl.ToolTip.IndexOf("<") >= 0 && hl.ToolTip.IndexOf(">") >= 0)
                {
                    int start = hl.ToolTip.IndexOf("<");
                    int end = hl.ToolTip.IndexOf(">");
                    if (start > end)
                    {
                        hl.ToolTip = hl.ToolTip.Replace(">", "");
                    }
                    else
                    {
                        string rep = hl.ToolTip.Substring(start, end - start + 1);
                        hl.ToolTip = hl.ToolTip.Replace(rep, "");
                    }
                    hl.ToolTip = hl.ToolTip.Replace("&nbsp;", "");
                    hl.ToolTip = hl.ToolTip.Replace("&#8212;", "—");
                    hl.ToolTip = hl.ToolTip.Replace("&#8216;", "‘");
                    hl.ToolTip = hl.ToolTip.Replace("&#8217;", "’");
                    hl.ToolTip = hl.ToolTip.Replace("&#8220;", "“");
                    hl.ToolTip = hl.ToolTip.Replace("&#8221;", "”");
                    hl.ToolTip = hl.ToolTip.Replace("&#8230;", "…");
                }
                if (hl.ToolTip.Length > 200)
                {
                    hl.ToolTip = hl.ToolTip.Substring(0, 200) + "......";
                }
                if (hl.Text.Length > 24)
                {
                    hl.Text = hl.Text.Substring(0, 24) + "...";
                }
                hl.NavigateUrl = string.Format("News.aspx?nid={0}", id);
                Label labtime = (Label)dlnewslist.Items[i].FindControl("labtime") as Label;
                DateTime t = Convert.ToDateTime(labtime.Text);
                labtime.Text = t.ToShortDateString();
            }
        }
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        string tid = em.Decript(Request.QueryString["tid"]);
        string mid = "0";
        if (ddltype.SelectedValue != "--请选择--")
        {
            mid = ddltype.SelectedValue;
        }
        string begintime = txtbegintime.Text;
        if (begintime == "")
        {
            begintime = "2011-1-1";
        }
        string endtime = txtendtime.Text;
        if (endtime == "")
        {
            endtime = DateTime.Now.ToString();
        }
        dt = nd.getNews(tid,mid,begintime,endtime);
        if (dt != null && dt.Rows.Count > 0)
        {
            DataView objView = dt.DefaultView;
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = objView;
            objPds.AllowPaging = true;
            objPds.PageSize = 19;
            objPds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());
            if (!objPds.IsFirstPage)
            {
                lbpre.Visible = true;
            }
            else
            {
                lbpre.Visible = false;
            }
            if (!objPds.IsLastPage)
            {
                lbnext.Visible = true;
            }
            else
            {
                lbnext.Visible = false;
            }
            dlnewslist.DataSource = objPds;
            dlnewslist.DataBind();
            for (int i = 0; i < objPds.Count; i++)
            {
                int size = Convert.ToInt32(ViewState["pageindex"]) * 19 + i;
                string id = dt.Rows[size][0].ToString();
                id = em.Encript(id);
                HyperLink hl = (HyperLink)dlnewslist.Items[i].FindControl("hlnews") as HyperLink;
                while (hl.ToolTip.IndexOf("<") >= 0 && hl.ToolTip.IndexOf(">") >= 0)
                {
                    int start = hl.ToolTip.IndexOf("<");
                    int end = hl.ToolTip.IndexOf(">");
                    if (start > end)
                    {
                        hl.ToolTip = hl.ToolTip.Replace(">", "");
                    }
                    else
                    {
                        string rep = hl.ToolTip.Substring(start, end - start + 1);
                        hl.ToolTip = hl.ToolTip.Replace(rep, "");
                    }
                    hl.ToolTip = hl.ToolTip.Replace("&nbsp;", "");
                    hl.ToolTip = hl.ToolTip.Replace("&#8212;", "—");
                    hl.ToolTip = hl.ToolTip.Replace("&#8216;", "‘");
                    hl.ToolTip = hl.ToolTip.Replace("&#8217;", "’");
                    hl.ToolTip = hl.ToolTip.Replace("&#8220;", "“");
                    hl.ToolTip = hl.ToolTip.Replace("&#8221;", "”");
                    hl.ToolTip = hl.ToolTip.Replace("&#8230;", "…");
                }
                if (hl.ToolTip.Length > 200)
                {
                    hl.ToolTip = hl.ToolTip.Substring(0, 200) + "......";
                }
                if (hl.Text.Length > 24)
                {
                    hl.Text = hl.Text.Substring(0, 24) + "...";
                }
                hl.NavigateUrl = string.Format("News.aspx?nid={0}", id);
                Label labtime = (Label)dlnewslist.Items[i].FindControl("labtime") as Label;
                DateTime t = Convert.ToDateTime(labtime.Text);
                labtime.Text = t.ToShortDateString();
            }
        }
        else
        {
            string script = "<script>window.alert('对不起，无此相关信息！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
    protected void ddltype_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["pageindex"] = "0";
    }
}
